/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import olympicgameslogic.*;
/**
 *
 * @author fabio mendonca
 */

public class Login extends JFrame {
  
   private JTextField txtuser = new JTextField(15);
   private JPasswordField txtpass = new JPasswordField(15); 
   private Boolean trigger = false;
   private JLabel lblerro = new JLabel("");
   private AppUser ut;
   
   public Login(String titulo){
        super(titulo);
        
        ClassLoader cldr = this.getClass().getClassLoader();
        super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png"))); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setBackground(Color.white);
        JPanel pannorte= new JPanel(new FlowLayout(FlowLayout.CENTER));
        pannorte.setSize(200,60); 
        pannorte.setBackground(Color.white);
        
        
        
        ImageIcon img = new ImageIcon(cldr.getResource("images/extra/topo_site.jpg"));
        JLabel backgound = new JLabel(img);
        
        
        pannorte.add(backgound);
        
        JPanel pancentro= new JPanel();
        pancentro.setBackground(Color.white);        
        pancentro.setLayout(new GridLayout(3, 1));
        
        JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));        
        p1.add(new JLabel("Username:"));             
        p1.add(txtuser);    
        p1.setBackground(Color.white);  
        pancentro.add(p1);  
        
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));  
        p2.add(new JLabel("Password:"));               
        p2.add(txtpass); 
        p2.setBackground(Color.white);  
        pancentro.add(p2); 
        
        JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p3.setBackground(Color.white);         
        
        p3.add(lblerro);
        lblerro.setFont(new Font("Arial", Font.ITALIC, 12));
        lblerro.setForeground(Color.red);
        lblerro.setFocusable(false);
        lblerro.setVisible(false);        
        pancentro.add(p3);
        
        
        JPanel pansul= new JPanel();
        pansul.setSize(200,20);
        pansul.setBackground(Color.white);  
        JButton btok = new JButton("OK", new ImageIcon(cldr.getResource("images/extra/btok.png")));
        JButton btcancel = new JButton("Cancel", new ImageIcon(cldr.getResource("images/extra/btcancel.png")));
        pansul.setLayout(new FlowLayout(FlowLayout.CENTER));                        
        pansul.add(btok);
        pansul.add(btcancel);        
        add(pannorte,BorderLayout.NORTH);
        add(pancentro,BorderLayout.CENTER);
        add(pansul,BorderLayout.SOUTH);        
        setSize(340,270);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);                
        
        TrataEvento t = new TrataEvento();        
        
	btok.addActionListener(t);
	
	btcancel.addActionListener(new ActionListener() {
            @Override
	    public void actionPerformed(ActionEvent e) {		
		dispose();                		
	    }
	});        
}    
        private  class TrataEvento implements ActionListener {
        @Override
            public void actionPerformed(ActionEvent e) {               
                   procLogin();                            		
            }
        }                 
        
        private void procLogin(){
            for (AppUser appUser : OlympicGamesGui.ap.getAppUsers()) {
                    String p = new String(txtpass.getPassword());                    
                    if (txtuser.getText().toString().equalsIgnoreCase(appUser.getUsername().toString()) &&
                            p.equals(appUser.getPassword().toString())){                                                                        
                        trigger = true; 
                        OlympicGamesGui.ap.setUtilizador(appUser);                        
                    }      
                }
               if (trigger) { 
                   new JanelaPrincipal("Olympic Games Management / LAPRII");
                   dispose();                   
               }else {
                   txtuser.setText("");
                   txtpass.setText("");
                   txtuser.requestFocus();
                   lblerro.setVisible(true);
                   lblerro.setText("Invalid Username or Password");                   
               } 
        }
}
